﻿using Pb;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DDZCardsControl : MonoBehaviour {
    EginUser user = EginUser.Instance;
    EginDDZ ddz = EginDDZ.Instance;
    public GameObject ownCardsFather;
    public List<UISprite> ownCard;
    public GameObject LeftCard;
    public List<UISprite> LeftRemainderCards;
    public GameObject rightCard;
    public List<UISprite> rightRemainderCards;
    public List<int> ownCardsInControl;
    int leftRemainder;
    int rightRemainder;

    //出牌按钮
    public GameObject outCardBtnObj;
    public GameObject tishi;
    public GameObject outBtn;
    public GameObject noOutBtn;
    public GameObject NotOutCardBtn;  //要不起
    public GameObject CancelAutoBtnFather;  //取消托管
    public GameObject CancelAutoBtn;  //取消托管
    //牌的控制
    public POKER_TYPE lastPokerType;
    public int lastSeatId;
    public List<int> lastOutCards = new List<int>();


    //出出去的牌显示
    public List<GameObject> PlayerOutCardFatherList;
    public List<DDZPlayerOutCard> ddzPlayerOutCardList;
    public List<GameObject> PassStatusList;
    void Awake()
    {
        UIEventListener.Get(tishi).onClick = ButtonClick;
        UIEventListener.Get(outBtn).onClick = ButtonClick;
        UIEventListener.Get(noOutBtn).onClick = ButtonClick;
        UIEventListener.Get(NotOutCardBtn).onClick = ButtonClick;
        UIEventListener.Get(CancelAutoBtn).onClick = ButtonClick;

    }
    void ButtonClick(GameObject button)
    {
        if (button.name == "0")
        {
            //提示
            PromptEvent();
        }
        else if (button.name == "1")
        {
            //出牌
            OutCardEvent();
            //将button的碰撞取消
            outBtn.GetComponent<UIButton>().enabled = false;
        }
        else if (button.name == "2")
        {
            //不出
            List<int> selectCards = new List<int>();
            SendDdzPlayRequest(POKER_TYPE.Pass, selectCards);
            outCardBtnObj.SetActive(false);
            for (int i = 0; i < ownCard.Count; i++)
            {
                ownCard[i].gameObject.GetComponent<DDZCardButton>().Select = false;
                ownCard[i].gameObject.transform.localPosition = new Vector3(ownCard[i].gameObject.transform.localPosition.x, 0, 0);
            }
        }
        else if (button.name == "NotOutCardBtn")
        {
            //要不起
            List<int> selectCards = new List<int>();
            SendDdzPlayRequest(POKER_TYPE.Pass, selectCards);
            NotOutCardBtn.SetActive(false);
        }

        else if (button.name == "CancelAutoBtn")
        {
            Debug.Log("quxiaotuoguan");
            UserCancelRobotRequest lg = new Pb.UserCancelRobotRequest { };
            SocketManager socketManager = SocketManager.Instance;
            socketManager.SendProtobuf1(lg, MsgId.UserCancelRobotRequest);
            CancelAutoBtnFather.SetActive(false);
        }
    }
    public void InitBtn() {
        PromptCount = 0;
        promptCards.Clear();//把提示出牌使用的字典索引 重置为0

        outCardBtnObj.SetActive(false);
        NotOutCardBtn.SetActive(false);
        outBtn.GetComponent<UIButton>().enabled = true;
        noOutBtn.SetActive(true);
        outBtn.transform.localPosition = new Vector3(306,-57,0);
        tishi.transform.localPosition = new Vector3(-69,-57,0);
    }
    /// <summary>
    /// 发牌叫分阶段
    /// </summary>
    public void StartShowCards(List<int> ownCards) {
        //显示乱序
        //再显示背面
        //显示正确排序
        ownCards = SortCardForDDZ(ownCards);
        ownCardsInControl = ownCards;
        StartCoroutine( FirstShowCard());
        //其他玩家剩余牌显示
        leftRemainder = 17;
        rightRemainder = 17;
        LeftRemainderCards[1].spriteName = "7";
        rightRemainderCards[1].spriteName = "7";
    }
    /// <summary>
    /// 斗地主重连显示牌
    /// </summary>
    /// <param name="ownCards"></param>
    public void ReconnectShowCards(List<int> ownCards) {
        ownCards = SortCardForDDZ(ownCards);
        ownCardsInControl = ownCards;
        ShowCard();
    }

    /// <summary>
    /// 地主添加三张牌
    /// </summary>
    /// <param name="cards"></param>
    public void AddThreeCards(List<int> cards) {
        for (int i = 0; i < cards.Count; i++) {
            ownCardsInControl.Add(cards[i]);
        }
        ownCardsInControl= SortCardForDDZ(ownCardsInControl);
        ShowCard();
    }

    /// <summary>
    /// 玩家出牌处理
    /// </summary>
    /// <param name="cards"></param>
    public void RemoveOutCards(List<int> cards) {
        if (cards.Count > 0) {
            for (int i = 0; i < cards.Count; i++)
            {
                ownCardsInControl.Remove(cards[i]);
            }
            ShowCard();
        }
    }

    #region 其他玩家剩余牌显示
    /// <summary>
    /// 其他玩家剩余牌显示
    /// </summary>
    /// <param name="remainder"></param>
    /// <param name="otherSid"></param>
    public void OtherCardRemainderShow(int remainder, int otherSid) {
        if (ddz.seatId == 1)
        {
            if (otherSid == 2) {
                leftRemainder += remainder;
                ShowLeftRemainder();
            }
            else if (otherSid == 3)
            {
                rightRemainder += remainder;
                ShowRightRemainder();
            }
        }
        else if (ddz.seatId == 2)
        {
            if (otherSid == 3)
            {
                leftRemainder += remainder;
                ShowLeftRemainder();
            }
            else if (otherSid == 1)
            {
                rightRemainder += remainder;
                ShowRightRemainder();
            }
        }
        else if (ddz.seatId == 3)
        {
            if (otherSid == 1)
            {
                leftRemainder += remainder;
                ShowLeftRemainder();
            }
            else if (otherSid == 2)
            {
                rightRemainder += remainder;
                ShowRightRemainder();
            }
        }
    }
    void  ShowLeftRemainder()
    {
        
        string tempstr = leftRemainder.ToString();
        Debug.Log("leftRemainder"+ tempstr);
        if (leftRemainder >= 10)
        {
            
            LeftRemainderCards[0].spriteName = tempstr.Substring(0,1);
            LeftRemainderCards[1].spriteName = tempstr.Substring(1,1);
        }
       else 
       {          
            LeftRemainderCards[0].spriteName = tempstr;
            LeftRemainderCards[1].gameObject.SetActive(false);
            LeftRemainderCards[0].gameObject.transform.localPosition = new Vector3(35,
                                                                                    LeftRemainderCards[0].gameObject.transform.localPosition.y,
                                                                                    0);
        }
    }
    void ShowRightRemainder()
    {
        string tempstr = rightRemainder.ToString();
        Debug.Log("rightRemainder" + tempstr);
        if (rightRemainder >= 10)
        {
            rightRemainderCards[0].spriteName = tempstr.Substring(0, 1);
            rightRemainderCards[1].spriteName = tempstr.Substring(1, 1);
        }
        else
        {           
            rightRemainderCards[0].spriteName = tempstr;
            rightRemainderCards[1].gameObject.SetActive(false);
            rightRemainderCards[0].gameObject.transform.localPosition = new Vector3(35, 
                                                                                    rightRemainderCards[0].gameObject.transform.localPosition.y, 
                                                                                    0);
        }
    }
    #endregion

    IEnumerator FirstShowCard()
    {
        for (int i = 0; i < ownCard.Count; i++)
        {
            ownCard[i].gameObject.SetActive(false);
            ownCard[i].GetComponent<DDZCardButton>().Select = false;
            ownCard[i].gameObject.transform.localPosition = new Vector3(ownCard[i].transform.localPosition.x, 0, 0);
        }
        int j = 0;
        int tempInt = 20 - ownCardsInControl.Count;
        for (int i = 20 - ownCardsInControl.Count; i < ownCardsInControl.Count+ tempInt; i++)
        {
            ownCard[i].gameObject.SetActive(true);
            ownCard[i].gameObject.name = ownCardsInControl[j].ToString();
            ownCard[i].spriteName = ownCardsInControl[j].ToString();
            ownCard[i].gameObject.GetComponent<UIButton>().normalSprite = ownCardsInControl[j].ToString();
            float x = -465 - (20 - ownCardsInControl.Count-2) * (float)22.5;
            ownCardsFather.transform.localPosition = new Vector3(x, ownCardsFather.transform.localPosition.y, 0);
            yield return new WaitForSeconds(0.1f);
            j++;

        }
        float xx = -465 - (20 - ownCardsInControl.Count-2) * (float)22.5;
        ownCardsFather.transform.localPosition = new Vector3(xx, ownCardsFather.transform.localPosition.y, 0);
    }
    /// <summary>
    /// 重新显示card排序
    /// </summary>
    void ShowCard() {
        for (int i = 0; i < ownCard.Count; i++) {
            ownCard[i].gameObject.SetActive(false);
            ownCard[i].GetComponent<DDZCardButton>().Select = false;
            ownCard[i].gameObject.transform.localPosition = new Vector3(ownCard[i].transform.localPosition.x, 0, 0);
        }
        int j = 0;
        int tempInt = 20 - ownCardsInControl.Count;
        for (int i = tempInt; i < ownCardsInControl.Count+ tempInt; i++)
        {
            //Debug.Log(ownCardsInControl[i].ToString());
            ownCard[i].gameObject.SetActive(true);
            ownCard[i].gameObject.name= ownCardsInControl[j].ToString();
            ownCard[i].spriteName = ownCardsInControl[j].ToString();
            ownCard[i].gameObject.GetComponent<UIButton>().normalSprite = ownCardsInControl[j].ToString();
            j++;

        }
        float x = -465 - (20 - ownCardsInControl.Count-2) * (float)22.5;
        ownCardsFather.transform.localPosition = new Vector3(x, ownCardsFather.transform.localPosition.y, 0);
    }

    /// <summary>
    /// 显示 出牌 不出 提示 要不起按钮
    /// </summary>
    public void ShowOutCardBtnObj() {
        //判断上家是不是自己 是的话显示出牌 不是判断是否要的起->要的起显示出牌 -要不起显示要不起按钮
        if (lastSeatId != 0)
        {
            if (lastSeatId == ddz.seatId)
            {
                noOutBtn.SetActive(false);
                outBtn.transform.localPosition = new Vector3(214, -57, 0);
                tishi.transform.localPosition = new Vector3(37, -57, 0);
                outCardBtnObj.SetActive(true);
            }
            else
            {              
                //判断是否显示不出按钮
                List<int> myCards = new List<int>();
                for (int i = 0; i < ownCard.Count; i++)
                {
                    if (ownCard[i].gameObject.activeSelf)
                    {
                        myCards.Add((int.Parse)(ownCard[i].spriteName));
                    }
                }
                myCards = SortCardForRule(myCards);
                List<int> tempLastOutCards = new List<int>();
                tempLastOutCards = SortCardForRule(lastOutCards);
                if (DDZCardCompare.isShowOutCardBtn(myCards, tempLastOutCards, lastPokerType))
                {
                    Debug.Log("true lastPokerType" + lastPokerType);
                    Debug.Log("true 显示能出牌");
                    outCardBtnObj.SetActive(true);
                }
                else
                {
                    Debug.Log("false lastPokerType" + lastPokerType);
                    Debug.Log("false 显示要不起");
                    NotOutCardBtn.SetActive(true);
                }
            }
        }
        else {
            noOutBtn.SetActive(false);
            outBtn.transform.localPosition = new Vector3(214, -57, 0);
            tishi.transform.localPosition = new Vector3(37, -57, 0);
            outCardBtnObj.SetActive(true);
        }
    }

    /// <summary>
    /// 出牌事件
    /// </summary>
    void OutCardEvent() {
        //判断选中的牌
        List<int> selectCards = new List<int>();
        for (int i=0;i< ownCard.Count;i++) {
            if (ownCard[i].gameObject.activeSelf) {
                DDZCardButton dcb = ownCard[i].GetComponent<DDZCardButton>();
                if (dcb.Select) {
                    selectCards.Add((int.Parse)(ownCard[i].spriteName));
                } 
            }
        }
        
        if (selectCards.Count > 0)
        {
            List<int> currentcards = new List<int>();
            currentcards = SortCardForRule(selectCards);
           
            POKER_TYPE currentType = POKER_TYPE.Pass;
            //判断牌型 和选中牌型是否能出牌
            bool isRule = DDZCardRule.PopEnable(currentcards, out currentType);
            Debug.Log("isRule" + isRule);
            Debug.Log("currentType" + currentType);
            Debug.Log("lastSeatId:" + lastSeatId);
            if (lastSeatId != ddz.seatId&& lastSeatId!=0) //判断是否需要压住上家的牌
            {
                
                if (!isRule)
                {
                    user.logMsg = "你选择牌的牌型不正确，请重新选择3";
                    LogMsg();
                   
                }
                else
                {
                    List<int> tempLastOutCards = new List<int>();
                    tempLastOutCards = SortCardForRule(lastOutCards);
                    //判断是否大于上家牌
                    if (DDZCardCompare.isSelectCardCanPut(currentcards, currentType, tempLastOutCards, lastPokerType))
                    {
                        SendDdzPlayRequest(currentType, selectCards);
                        //隐藏button
                        outCardBtnObj.SetActive(false);
                    }
                    else
                    {
                        user.logMsg = "你选择牌的牌不能压住上家，请重新选择";
                        LogMsg();
                    }
                }

            }
            else
            {
                //判断是否是牌型 直接发送牌型 和选中的牌
                if (isRule)
                {
                    SendDdzPlayRequest(currentType, selectCards);
                    //隐藏button
                    outCardBtnObj.SetActive(false);
                }
                else
                {
                    //提示牌型不对
                    user.logMsg = "你选择牌的牌型不正确，请重新选择1";
                    LogMsg();
                   
                }
            }
        }
        else {
            user.logMsg = "请选择牌";
            LogMsg();
        }
        outBtn.GetComponent<UIButton>().enabled = true;
    }


    #region 提示出牌
    int PromptCount =0;
    Dictionary<int, List<int>> promptCards = new Dictionary<int, List<int>>();
    /// <summary>
    /// 提示出牌
    /// </summary>
    void PromptEvent() {
        if (promptCards.Count==0)
        {
            List<int> myCards = new List<int>();
            for (int i = 0; i < ownCard.Count; i++)
            {
                if (ownCard[i].gameObject.activeSelf)
                {
                    myCards.Add((int.Parse)(ownCard[i].spriteName));
                }
            }
            myCards = SortCardForRule(myCards);
            List<int> tempLastOutCards = new List<int>();
            if (lastSeatId == ddz.seatId || lastSeatId == 0)
            {
                Debug.Log("地主第一次出牌");
                promptCards = DDZCardCompare.FindPromptCards(myCards, tempLastOutCards, POKER_TYPE.Pass);
            }
            else
            {
                tempLastOutCards = SortCardForRule(lastOutCards);
                promptCards = DDZCardCompare.FindPromptCards(myCards, tempLastOutCards, lastPokerType);
            }

        }
        
        for (int i = 0; i < ownCard.Count; i++) {
            ownCard[i].gameObject.GetComponent<DDZCardButton>().Select=false;
            ownCard[i].gameObject.transform.localPosition = new Vector3(ownCard[i].gameObject.transform.localPosition.x,0,0);
        }
        /*查找牌值 显示到上*/
        for (int i=0;i< promptCards[PromptCount].Count;i++) {
            for (int j = 0; j < ownCard.Count; j++) {
                if (ownCard[j].gameObject.activeSelf&& ownCard[j].gameObject.GetComponent<DDZCardButton>().Select==false)
                {
                    int tempInt = int.Parse(ownCard[j].spriteName);
                    if (tempInt % 13 == promptCards[PromptCount][i]
                        && tempInt % 13 != 0 && tempInt != 53 && tempInt != 54)
                    {
                        ownCard[j].gameObject.GetComponent<DDZCardButton>().PromptInit();
                        Debug.Log("提示" + PromptCount + "次");
                        break;
                    }
                    else if(tempInt % 13==0&& promptCards[PromptCount][i]==13) {
                        ownCard[j].gameObject.GetComponent<DDZCardButton>().PromptInit();
                        Debug.Log("提示" + PromptCount + "次");
                        break;
                    }
                    else if (tempInt  == 53 && promptCards[PromptCount][i] == 53)
                    {
                        ownCard[j].gameObject.GetComponent<DDZCardButton>().PromptInit();
                        Debug.Log("提示" + PromptCount + "次");
                        break;
                    }
                    else if (tempInt == 54 && promptCards[PromptCount][i] == 54)
                    {
                        ownCard[j].gameObject.GetComponent<DDZCardButton>().PromptInit();
                        Debug.Log("提示" + PromptCount + "次");
                        break;
                    }
                }
            }
        }
        Debug.Log( PromptCount + "次"+"---总次数"+ promptCards.Count);
        PromptCount++;
        if (PromptCount == promptCards.Count ) {
            PromptCount = 0;
        }
    }
    #endregion 提示出牌

    #region 显示所有玩家的出牌
    /// <summary>
    /// 显示所有玩家的出牌
    /// </summary>
    public void ShowPlayerOutCard(List<int> cards,int tempSeatId,POKER_TYPE pokerType,int nextSid) {
        //当前出牌的显示
        int tempIndex = ddz.returnOtherSeatIndex(tempSeatId);
        PlayerOutCardFatherList[tempIndex].SetActive(true);
        if (pokerType == POKER_TYPE.Pass)
        {
            PassStatusList[tempIndex].SetActive(true);
        }
        else
        {
            //排序
            cards = SortCardForShowUI(cards);  //从小到大排
            if (pokerType == POKER_TYPE.TripleWithSingle || pokerType == POKER_TYPE.TripleWithTwin ||
               pokerType == POKER_TYPE.FourWithSingle || pokerType == POKER_TYPE.FourWithTwin ||
              pokerType == POKER_TYPE.PlaneWithSingle || pokerType == POKER_TYPE.PlaneWithTwin )
            {
                //先排四张 再三张 再两张
                cards = SortCardForShowUIFourOrThreeSame(cards);
            }


           
            // 自己玩家需要进行位移判断
            if (tempIndex == 0)
            {
                float x = -32 - cards.Count * 15;
                PlayerOutCardFatherList[tempIndex].transform.localPosition = new Vector3(x, 0, 0);
                //赋值
                for (int i = 0; i < cards.Count; i++)
                {
                    ddzPlayerOutCardList[tempIndex].playerOutCards[i].gameObject.SetActive(true);
                    ddzPlayerOutCardList[tempIndex].playerOutCards[i].spriteName = cards[i].ToString();
                }
            }
            else if (tempIndex == 1) {
                if (cards.Count <= 9)
                {
                    for (int i = 0; i < cards.Count; i++)
                    {
                        int tempInt = 9 - (cards.Count - i); 
                        ddzPlayerOutCardList[tempIndex].playerOutCards[tempInt].gameObject.SetActive(true);
                        ddzPlayerOutCardList[tempIndex].playerOutCards[tempInt].spriteName = cards[i].ToString();
                    }
                }
                else {
                    //赋值
                    for (int i = 0; i < cards.Count; i++)
                    {
                        ddzPlayerOutCardList[tempIndex].playerOutCards[i].gameObject.SetActive(true);
                        ddzPlayerOutCardList[tempIndex].playerOutCards[i].spriteName = cards[i].ToString();
                    }
                }
                
            }
            else if (tempIndex == 2)
            {
                //赋值
                for (int i = 0; i < cards.Count; i++)
                {
                    ddzPlayerOutCardList[tempIndex].playerOutCards[i].gameObject.SetActive(true);
                    ddzPlayerOutCardList[tempIndex].playerOutCards[i].spriteName = cards[i].ToString();
                }
            }

        }

        //下一个出牌玩家进行物体PlayerOutCard初始化 隐藏父物体 隐藏所有子物体 
        int tempIndex1 = ddz.returnOtherSeatIndex(nextSid);
        if (PassStatusList[tempIndex1].activeSelf)
        {
            PassStatusList[tempIndex1].SetActive(false);
            PlayerOutCardFatherList[tempIndex1].SetActive(false);
        }
        else {
            for (int i = 0; i < 20; i++)
            {
                ddzPlayerOutCardList[tempIndex1].playerOutCards[i].gameObject.SetActive(false);
            }
            if (tempIndex1 == 0)
            {
                PlayerOutCardFatherList[tempIndex1].transform.localPosition = new Vector3(-32, 0, 0);
            }
            PlayerOutCardFatherList[tempIndex1].SetActive(false);
        }
    }
    #endregion

    #region  游戏结束显示所有 牌
    /// <summary>
    /// 游戏结束显示所有 牌
    /// </summary>
    /// <param name="cards"></param>
    /// <param name="tempSeatId"></param>
    /// <param name="pokerType"></param>
    public void ShowCardForGameover(List<int> cards, int tempSeatId)
    {
        //隐藏出牌状态
        int tempIndex = ddz.returnOtherSeatIndex(tempSeatId);
        if (PassStatusList[tempIndex].activeSelf)
        {
            PassStatusList[tempIndex].SetActive(false);
            PlayerOutCardFatherList[tempIndex].SetActive(false);
        }
        else
        {
            for (int i = 0; i < 20; i++)
            {
                ddzPlayerOutCardList[tempIndex].playerOutCards[i].gameObject.SetActive(false);
            }
            if (tempIndex == 0)
            {
                PlayerOutCardFatherList[tempIndex].transform.localPosition = new Vector3(-32, 0, 0);
            }
            PlayerOutCardFatherList[tempIndex].SetActive(false);
        }

        //当前出牌的显示
        if (tempSeatId != ddz.seatId) {
            PlayerOutCardFatherList[tempIndex].SetActive(true);

            //排序
            cards = SortCardForShowUI(cards);  //从小到大排

            // 自己玩家需要进行位移判断
            if (tempIndex == 0)
            {
                float x = -32 - cards.Count * 15;
                PlayerOutCardFatherList[tempIndex].transform.localPosition = new Vector3(x, 0, 0);
                //赋值
                for (int i = 0; i < cards.Count; i++)
                {
                    ddzPlayerOutCardList[tempIndex].playerOutCards[i].gameObject.SetActive(true);
                    ddzPlayerOutCardList[tempIndex].playerOutCards[i].spriteName = cards[i].ToString();
                }
            }
            else if (tempIndex == 1)
            {
                if (cards.Count <= 9)
                {
                    for (int i = 0; i < cards.Count; i++)
                    {
                        int tempInt = 9 - (cards.Count - i);
                        ddzPlayerOutCardList[tempIndex].playerOutCards[tempInt].gameObject.SetActive(true);
                        ddzPlayerOutCardList[tempIndex].playerOutCards[tempInt].spriteName = cards[i].ToString();
                    }
                }
                else
                {
                    //赋值
                    for (int i = 0; i < cards.Count; i++)
                    {
                        ddzPlayerOutCardList[tempIndex].playerOutCards[i].gameObject.SetActive(true);
                        ddzPlayerOutCardList[tempIndex].playerOutCards[i].spriteName = cards[i].ToString();
                    }
                }

            }
            else if (tempIndex == 2)
            {
                //赋值
                for (int i = 0; i < cards.Count; i++)
                {
                    ddzPlayerOutCardList[tempIndex].playerOutCards[i].gameObject.SetActive(true);
                    ddzPlayerOutCardList[tempIndex].playerOutCards[i].spriteName = cards[i].ToString();
                }
            }
        }
        

        
        
    }

    #endregion

    void LogMsg() {
        GameObject logMsg = NGUITools.AddChild(GameObject.Find("UI Root"), (GameObject)(Resources.Load("LogMsg")));
    }
    



    //发送服务器消息
    void SendDdzPlayRequest(POKER_TYPE ddzPokerType,List<int> selectCards) {
        DdzPlayRequest lg = new Pb.DdzPlayRequest();
        lg.PokerType = ddzPokerType;
        if (selectCards.Count == 0)
        {
            
        }
        else {
            for (int i = 0; i < selectCards.Count; i++) {
                lg.CardList.Add(selectCards[i]);
            }
        }
        SocketManager socketManager = SocketManager.Instance;
        socketManager.SendProtobuf1(lg, MsgId.DdzPlayRequest);
    }

    #region DDZ UI显示排序 从大到小排序
    /// <summary>
    ///DDZ UI显示排序 从大到小排序
    /// </summary>
    List<int> SortCardForDDZ(List<int> ownCards)
    {
        List<int> temp = new List<int>();
        List<int> temp1 = new List<int>();
        ownCards.Sort();

        for (int i = 0; i < ownCards.Count; i++)
        {
            if (ownCards[i] % 13 == 0)
            {
                Debug.Log("有2");
                temp1.Add(ownCards[i]);
            }
        }

        for (int i = 0; i < ownCards.Count; i++)
        {
            if (ownCards[i] == 53)
            {
                temp1.Add(53);
                ownCards.RemoveAt(i);
            }
        }
        for (int i = 0; i < ownCards.Count; i++)
        {
            if (ownCards[i] == 54)
            {
                temp1.Add(54);
                ownCards.RemoveAt(i);
            }
        }

        for (int i = 12; i > 0; i--)
        {
            for (int j = 0; j < ownCards.Count; j++)
            {
                if (ownCards[j] % 13 == i)
                {
                    temp.Add(ownCards[j]);
                }
            }
        }

        for (int i = 0; i < temp1.Count; i++)
        {
            temp.Insert(0, temp1[i]);
        }
        return temp;
    }

    #endregion DDZ式排序

    #region 算法使用排序 从小到大排序 出去去的牌的UI显示排序除了三带 和四带都能用
    /// <summary>
    /// 为算法使用排序  直接从小到大排序
    /// </summary>
    /// <param name="ownCards"></param>
    /// <returns></returns>
    List<int> SortCardForRule(List<int> cards)
    {
        List<int> temp = new List<int>();
        if (cards.Count > 0) {         
            for (int i = 1; i < 13; i++)
            {
                for (int j = 0; j < cards.Count; j++)
                {
                    if (cards[j] != 53 && cards[j] != 54) {
                        int tempInt = cards[j] % 13;
                        if (tempInt == i)
                        {
                            temp.Add(tempInt);
                        }
                    }
                }
            }
            for (int j = 0; j < cards.Count; j++)
            {
                int tempInt = cards[j] % 13;
                if (tempInt == 0)
                {
                    temp.Add(13);
                }
            }
            for (int j = 0; j < cards.Count; j++)
            {

                if (cards[j] == 53)
                {
                    temp.Add(cards[j]);
                }
            }
            for (int j = 0; j < cards.Count; j++)
            {

                if (cards[j] == 54)
                {
                    temp.Add(cards[j]);
                }
            }
        }
        

        return temp;
    }


    #endregion

    #region 从小到大排序 出去去的牌的UI显示排序除了三带 和四带都能用
    /// <summary>
    /// 为算法使用排序  直接从小到大排序
    /// </summary>
    /// <param name="ownCards"></param>
    /// <returns></returns>
    List<int> SortCardForShowUI(List<int> cards)
    {
        List<int> temp = new List<int>();
        if (cards.Count > 0)
        {
            for (int i = 1; i < 13; i++)
            {
                for (int j = 0; j < cards.Count; j++)
                {
                    if (cards[j] != 53 && cards[j] != 54)
                    {
                        int tempInt = cards[j] % 13;
                        if (tempInt == i)
                        {
                            temp.Add(cards[j]);
                        }
                    }
                }
            }
            for (int j = 0; j < cards.Count; j++)
            {
                int tempInt = cards[j] % 13;
                if (tempInt == 0)
                {
                    temp.Add(cards[j]);
                }
            }
            for (int j = 0; j < cards.Count; j++)
            {

                if (cards[j] == 53)
                {
                    temp.Add(cards[j]);
                }
            }
            for (int j = 0; j < cards.Count; j++)
            {

                if (cards[j] == 54)
                {
                    temp.Add(cards[j]);
                }
            }
        }


        return temp;
    }


    #endregion

    #region ui排序  出去去的牌的UI显示排序三带 和四带能用
    /// <summary>
    /// 为ui使用排序  
    /// </summary>
    /// <param name="ownCards"></param>
    /// <returns></returns>
    List<int> SortCardForShowUIFourOrThreeSame(List<int> cards)
    {
        List<int> temp = new List<int>();
        List<int> tempFourList = new List<int>();
        List<int> tempThreeList = new List<int>();
        List<int> tempTwoList = new List<int>();
        List<int> tempOneList = new List<int>();
        List<int> tempJoker = new List<int>();
        for (int i = 0; i < cards.Count; i++)
        {
            if (cards[i] == 53)
            {
                tempJoker.Add(cards[i]);
                cards.RemoveAt(i);
            }
        }
        for (int i = 0; i < cards.Count; i++)
        {
            if (cards[i] == 54)
            {
                tempJoker.Add(cards[i]);
                cards.RemoveAt(i);
            }
        }
        for (int i = 0; i < cards.Count; i++)
        {
            int tempInt = 0;
            for (int j = 0; j < cards.Count; j++)
            {

                if (cards[i]%13 == cards[j]%13)
                {
                    tempInt++;
                }

            }
            if (tempInt == 4)
            {
                tempFourList.Add(cards[i]);
            }
            if (tempInt == 3)
            {
                tempThreeList.Add(cards[i]);
            }
            else if (tempInt == 2)
            {
                tempTwoList.Add(cards[i]);
            }
            else if (tempInt == 1)
            {
                tempOneList.Add(cards[i]);
            }

        }
        if (tempFourList.Count > 0)
        {
            for (int i = 0; i < tempFourList.Count; i++)
            {
                temp.Add(tempFourList[i]);
            }
        }
        if (tempThreeList.Count > 0)
        {
            for (int i = 0; i < tempThreeList.Count; i++)
            {
                temp.Add(tempThreeList[i]);
            }
        }
        if (tempTwoList.Count > 0)
        {
            for (int i = 0; i < tempTwoList.Count; i++)
            {
                temp.Add(tempTwoList[i]);
            }
        }
        if (tempOneList.Count > 0) {
            for (int i = 0; i < tempOneList.Count; i++) {
                temp.Add(tempOneList[i]);
            }
        }
        if (tempJoker.Count > 0)
        {
            for (int i = 0; i < tempJoker.Count; i++)
            {
                temp.Add(tempJoker[i]);
            }
        }
        return temp;
    }


    #endregion

}
